import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/bezier_bounce_footer.dart';
import 'package:flutter_easyrefresh/bezier_hour_glass_header.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:village_android/model/menu_model.dart';
import 'package:village_android/model/rental_model.dart';
import 'package:village_android/pages/browser.dart';
import 'package:village_android/utils/http_utlis.dart';

class RentalManagerPage extends StatefulWidget {
  MenuModel menuModel;

  RentalManagerPage({Key key, this.menuModel}) : super(key: key);

  @override
  _RentalManagerPageState createState() => _RentalManagerPageState();
}
//NewsCell

class _RentalManagerPageState extends State<RentalManagerPage> {
  List items = [];

  @override
  void initState() {
    super.initState();
    getData();
  }

  void getData() {
    HttpUtils.get("/api/rental/list", data: {}, success: (data) {
      setState(() {
        items = data;
        print(items);
      });
    }, error: (errorMsg) {
      print(errorMsg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.menuModel.title),
        ),
        body:  Center(
                child: EasyRefresh(
                  header: BezierHourGlassHeader(backgroundColor: Colors.red),
                  footer: BezierBounceFooter(backgroundColor: Colors.red),
                  onRefresh: () async {
                    getData();
                  },
                  onLoad: () async {},
                  child: items.isEmpty
                      ? Container(
                      padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
                      child: Center(child: Text('无数据')))
                      : ListView.separated(
                      itemBuilder: (context, index) {
                        RentalModel rentalModel =
                            RentalModel.fromJson(items[index]);
                        return _cell(context, rentalModel);
                      },
                      separatorBuilder: (context, index) =>
                          Divider(height: 1.0, color: Colors.black12),
                      itemCount: items.length),
                ),
              ),
      ),
    );
  }

  void onItemClick(item) {
//    var page = new BrowserPage(title:item.title,url:item.url,);
//    Navigator.push(context, new MaterialPageRoute(builder: (context) => page));
  }

  //独立Item
  Widget _cell(BuildContext context, RentalModel item) {
    Widget coverWidget(item) {
      return FadeInImage.assetNetwork(
        placeholder: 'assets/empty_image.png',
        image: item.coverUrl,
        width: ScreenUtil().setWidth(100),
        height: ScreenUtil().setHeight(120),
        fit: BoxFit.fitWidth,
      );
    }

    return new GestureDetector(
      onTap: () {
        onItemClick(item);
      },
      child: Container(
        height: ScreenUtil().setHeight(250),
        alignment: Alignment.center,
        padding: EdgeInsets.all(10.0),
        margin: EdgeInsets.all(10.0),
        color: Colors.white30,
        child: Row(
          children: <Widget>[
            coverWidget(item),
            Padding(padding: EdgeInsets.only(left: 15)),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Text(
                    item.title,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                  Text(
                    "地址:　" + item.address.toString(),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black54,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                  Text(
                    "户主:　" + item.username,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black54,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                  Text(
                    "价格: ￥" + item.price.toString(),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black54,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                ],
              ),
            ),
            Icon(Icons.arrow_forward_ios),
          ],
        ),
      ),
    );
  }
}
